﻿using System;
using System.Linq;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using HtScrum.Service.Data;
using HtScrum.Service.Model;
using HtScrum.UI.Catalog.Project;
using HtScrum.Service.Repository;
using HtScrum.Service.Locator;
namespace HtScrum.UI.Catalog.Project.ViewModel
{
    public class ProjectDataItemViewModel : ViewModelBase
    {
        public ProjectDataItemViewModel(IServiceProvider serviceProvider)
        {
            this.ServiceProvider = serviceProvider;
        }

        #region Public Properties
        private EntityContract<ProjectModel> _EntityModel = null;
        public EntityContract<ProjectModel> EntityModel
        {
            get
            {
                return _EntityModel;
            }

            set
            {
                _EntityModel = value;
                this.Refresh();
            }
        }
        private IRepository<CustomerModel> customerRepository = RepositoryProviderLocator.Current.GetInstance<IRepository<CustomerModel>>();
        #endregion

        #region Commands
        public ICommand DeleteCommand { get { return new RelayCommand(ExecuteDeleteCommand); } }
        #endregion

        #region Public Methods
        public void ExecuteDeleteCommand()
        {
            ProjectDataCollectionViewModel customerDataCollectionViewModel = this.ServiceProvider.GetService(typeof(ProjectDataCollectionViewModel)) as ProjectDataCollectionViewModel;
            customerDataCollectionViewModel.DataCollection.Where(c => c == this.EntityModel).FirstOrDefault().State = eEntityState.Delete;
            customerDataCollectionViewModel.ViewModelCollection.Remove(this);
        }
        public void LoadCustomers(Action<bool> callback)
        {
            customerRepository.Load((result) =>
            {
                this.Customers = result;
            });
        }
        #endregion

        public void Refresh()
        {
            RaisePropertyChanged("EntityModel");
        }

        public IServiceProvider ServiceProvider { get; set; }

        private EntityContractCollection<CustomerModel> _Customers = null;
        public EntityContractCollection<CustomerModel> Customers
        {
            get
            {
                return this._Customers;
            }
            set
            {
                this._Customers = value;
                this.RaisePropertyChanged("Customers");
            }
        }
    }
}
